import react from "@vitejs/plugin-react";
import { resolve } from "path";
import { defineConfig } from "vite";
import vitePluginCompression from "vite-plugin-compression";

const baseUrl = "demo";

export default defineConfig((config) => {
  return {
    plugins: [
      react({
        jsxImportSource: "@emotion/react",
        babel: {
          plugins: ["@emotion/babel-plugin"],
        },
      }),
      vitePluginCompression({
        threshold: 1024 * 10, // 对大于 10kb 的文件进行压缩
        // deleteOriginFile: true,
      }),
    ],
    define: {
      "process.env": {},
    },
    resolve: {
      alias: {
        "@": `${resolve(process.cwd(), "src")}`,
      },
    },
    server: {
      open: true,
      port: 5794,
      // proxy: {
      //   "/api": {
      //     target: "http://localhost:8080",
      //     secure: false,
      //     rewrite: (path) => path.replace(/^\/api/, ""),
      //   },
      // },
    },
    // or use ./
    // base: config.mode === "development" ? "/" : `/${baseUrl}/`,
    base: `/${baseUrl}/`,
    build: {
      sourcemap: false,
      outDir: "dist",
      rollupOptions: {
        output: {
          chunkFileNames: "js/[name]-[hash].js", // 引入文件名的名称
          entryFileNames: "js/[name]-[hash].js", // 包的入口文件名称
          assetFileNames: "[ext]/[name]-[hash].[ext]", // 资源文件像 字体，图片等
          manualChunks(id) {
            if (id.includes("node_modules")) {
              return id
                .toString()
                .split("node_modules/")[1]
                .split("/")[0]
                .toString();
            }
          },
        },
      },
    },
  };
});
